//
//  SHModifyUserInfoViewController.m
//  NEWHYSJ
//
//  Created by CYF on 2017/3/25.
//  Copyright © 2017年 SH. All rights reserved.
//
#import "UserModel.h"
#import "SHModifyUserInfoViewController.h"

@interface SHModifyUserInfoViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)   UILabel     *changeLabel;
@property (nonatomic,strong)   UITextField *changeTf;
@property (nonatomic,strong)   UIButton    *determineBtn;
@property (nonatomic,strong)   UIView     *firstBgView;



@end

@implementation SHModifyUserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title  = @"修改昵称";
    
    [self configUI];
    
}
-(void)configUI{
    
    self.view.backgroundColor = KBASEBGCOLOR;
    self.firstBgView = [[UIView alloc]init];
    self.firstBgView.layer.borderWidth = 0.5;
    self.firstBgView.layer.borderColor = KLINECOLOR.CGColor;
    self.firstBgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.firstBgView];
    [self.firstBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(__I6Height(20));
        make.left.equalTo(self.view.mas_left).with.offset(__I6Width(0));
        make.right.equalTo(self.view.mas_right).with.offset(__I6Width(0));
        make.height.mas_equalTo(__I6Height(44));
    }];
    
    self.changeLabel = [[UILabel alloc]init];
    self.changeLabel.textColor = [UIColor colorWithHexString:@"#474958"];
    self.changeLabel.font = KSHSystemFont(12);
    [self.firstBgView addSubview:self.changeLabel];
    [self.changeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.firstBgView.mas_left).with.offset(0);
        make.centerY.mas_equalTo(self.firstBgView.mas_centerY);
        make.width.mas_equalTo(0);
        make.height.mas_equalTo(__I6Height(20));
    }];
    
    
    self.changeTf =  [[UITextField alloc]init];
    self.changeTf.delegate = self;
    self.changeTf.font = KSHSystemFont(14);
    self.changeTf.textColor = [UIColor blackColor];
    [self.changeTf  setValue:[UIColor colorWithHexString:@"#c0c0c7"] forKeyPath:@"_placeholderLabel.textColor"];
    [self.changeTf setValue:KSHSystemFont(14) forKeyPath:@"_placeholderLabel.font"];
    self.changeTf.clearButtonMode = UITextFieldViewModeAlways;
    [self.firstBgView addSubview:self.changeTf];
    [self.changeTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.changeLabel.mas_left).with.offset(__I6Width(15));
        make.right.mas_equalTo(self.firstBgView.mas_right).with.offset(__I6Width(-15));
        make.centerY.mas_equalTo(self.firstBgView.mas_centerY);
        make.height.mas_equalTo(__I6Height(42));
    }];
    
    
    self.changeTf.placeholder = @"请输入昵称";
    
    [self.firstBgView addSubview:self.changeTf];
    
    self.determineBtn = [[UIButton alloc]init];
    [self.determineBtn setBackgroundImage:[UIImage imageNamed:@"btn_bg"] forState:UIControlStateNormal];
    self.determineBtn.titleLabel.font = KSystemFont(16);
    [self.determineBtn setTitleColor:KSHTEXTCOLOR forState:UIControlStateNormal];
    [self.determineBtn setTitle:@"立即修改" forState:UIControlStateNormal];
    [self.determineBtn addTarget:self action:@selector(changeNickNameOrEmail:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview: self.determineBtn];
    [self.determineBtn  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.firstBgView.mas_bottom).offset(__I6Height(50));
        make.centerX.mas_equalTo(self.view);
    }];
    
    
}

- (void)changeNickNameOrEmail:(UIButton *)sender {
    [self.view endEditing:YES];
    if (self.changeTf.text.length == 0){
        [self.view makeToast:self.changeTf.placeholder duration:2 position:@"center"];
        return;
    }
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary *params = @{@"nickname":self.changeTf.text};
    
    [MyDataService requestAFURL:KChangeUser_info httpMethod:@"PUT" params:params data:nil complection:^(id result) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        if ([result[@"success"] isEqualToNumber:@(1)]) {
            [self.view makeToast:@"修改成功!" duration:2 position:@"center"];
            
            [UserModel shareManager].nickname = self.changeTf.text;
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if(_userInfoDelegate && [_userInfoDelegate respondsToSelector:@selector(startRefreshUserInfo)]){
                    
                    [_userInfoDelegate startRefreshUserInfo];
                }
                [self.navigationController popViewControllerAnimated:YES];
                
            });
            
        }
        
    } failure:^(id erro) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
    
}


#pragma mark --- 文本框代理

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end

